/****************************************************
	文件：RedSpotData.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/9/25 14:1:11
	功能：红点数据
*****************************************************/

using System;
using UnityEngine;
using HTUtility;

namespace RedSpotSystem
{
    public class RedSpotData
    {
        /// <summary>
        /// 当前红点数量
        /// </summary>
        public ReactiveProperty<int> Count;
        /// <summary>
        /// 所处模块
        /// </summary>
        public GEnum.E_GameModule GModule { get; set; }
        /// <summary>
        /// 父Panel枚举类型
        /// </summary>
        public GEnum.E_UIComp Parent { get; set; }
        /// <summary>
        /// 当前Panel枚举类型
        /// </summary>
        public GEnum.E_UIComp Self { get; set; }
        /// <summary>
        /// 是否为叶子节点
        /// </summary>
        public bool IsLeaf { get; set; }
        /// <summary>
        /// UI组件（View层）
        /// </summary>
        public UIComps Comps { get; set; }

        /// <summary>
        /// 更新前的红点数量
        /// </summary>
        private int mLastCount;
        /// <summary>
        /// 共外部注册的红点数量变化时的事件
        /// </summary>
        private RedSpotSys.RedSpotEvent mRedEvent;

        public RedSpotData()
        {
            Count = new ReactiveProperty<int>(0);
            Count.Value = 0;
            mLastCount = Count.Value;
            Count.Subscribe(OnRedNumChanged);
        }
        /// <summary>
        /// 重置红点数据
        /// </summary>
        public void Reset()
        {
            Count.Reset(0);
            UnsubscribeAll();
        }

        /// <summary>
        /// 绑定View层
        /// </summary>
        /// <param name="UIComps">UI组件</param>
        public RedSpotData Bind(UIComps uiComps)
        {
            Comps = uiComps;
            return this;
        }
        /// <summary>
        /// 注册红点变化时的事件
        /// </summary>
        /// <param name="redEvent">红点数量变化时的事件</param>
        public void Subscribe(RedSpotSys.RedSpotEvent redEvent)
        {
            mRedEvent += redEvent;
        }
        /// <summary>
        /// 注销红点变化时的事件
        /// </summary>
        /// <param name="redEvent">红点数量变化时的事件</param>
        public void Unsubscribe(RedSpotSys.RedSpotEvent redEvent)
        {
            mRedEvent -= redEvent;
        }
        /// <summary>
        /// 注销所有红点变化时的事件
        /// </summary>
        public void UnsubscribeAll()
        {
            mRedEvent = null;
        }

        /// <summary>
        /// 设置红点数量
        /// </summary>
        /// <param name="curNum">当前红点数量</param>
        public void SetRedNum(int curNum)
        {
            if (IsLeaf == false)
            {
                HTLogger.Error("非叶子节点不可设置红点，当前 UIPanel：{0}", Self.ToString());
            }
            else
            {
                Count.Value = curNum;
            }
        }

        /// <summary>
        /// 红点变化时的响应事件
        /// </summary>
        /// <param name="newNum"></param>
        private void OnRedNumChanged(int newNum)
        {
            if (Parent != GEnum.E_UIComp.Default)
            {
                RedSpotSys.Instance[Parent].AddRedNum(newNum - mLastCount);
            }
            mRedEvent?.Invoke(this);
            mLastCount = Count.Value;
        }
        /// <summary>
        /// 添加红点数量
        /// </summary>
        /// <param name="addNum">添加的红点数量</param>
        private void AddRedNum(int addNum)
        {
            Count.Value += addNum;
        }
    }
}
